package com.otod.sys.role.web;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonFailure;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.entity.Page;
import com.otod.commons.utils.Collections3;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.sys.role.service.IRoleService;
import com.otod.sys.vo.AuapRRoleMenuVo;
import com.otod.sys.vo.AuapTRoleVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 角色管理
 *
 * @author jqiu
 * @date 2014-02-19
 * update by superzoc 2014/05/15 修改更新角色菜单时候旧的菜单没有删除
 */
@Controller
@RequestMapping("sys/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    /**
     * 查询所有角色
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "findAll", method = RequestMethod.POST)
    @ResponseBody
    public List<AuapTRoleVo> findAll() {
        List<AuapTRoleVo> roleList = roleService.findAll();
        return roleList;
    }

    /**
     * 查询所有可选择的角色
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.POST)
    @ResponseBody
    public List<AuapTRoleVo> selectAll(HttpServletRequest request) {
        List<AuapTRoleVo> roleList = roleService.findAll();
        return roleList;
    }


    /**
     * 角色查询方法
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "query", method = RequestMethod.GET)
    @ResponseBody
    public List<AuapTRoleVo> query(HttpServletRequest request) {
        Map params = super.getParamMapFromRequest(request);
        return roleService.queryForList(params);
    }

    /**
     * 角色查询方法
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "queryPage", method = RequestMethod.GET)
    @ResponseBody
    public Page queryPage(HttpServletRequest request) {
        Map params = super.getParamMapFromRequest(request);
        Page page = new Page(request);
        return roleService.queryForList(page, params);
    }

    /**
     * 根据角色ID获取角色信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "getRoleById", method = RequestMethod.GET)
    @ResponseBody
    public AuapTRoleVo getRoleById(String id) {
        Map<String, String> map = new HashMap<String, String>();
        AuapTRoleVo vo = roleService.getById(id);
        return vo;
    }

    /**
     * 保存角色
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "saveRole", method = RequestMethod.POST)
    @ResponseBody
    public String saveRole(AuapTRoleVo vo) {
        if (null != vo) {
            if (null != vo.getUuid() && !"".equals(vo.getUuid())) {
                //更新
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("uuid", vo.getUuid());
                map.put("roleNo", vo.getRoleNo());
                map.put("roleName", vo.getRoleName());
                roleService.updateRole(map);
            } else {
                //新增
                roleService.addRole(vo);
            }
        }
        return JsonSuccess.factory("保存角色成功");
    }

    /**
     * 删除角色
     *
     * @param vo
     * @return
     * @throws AppException 
     */
    @RequestMapping(value = "delRoles", method = RequestMethod.GET)
    @ResponseBody
    public String delRole(String roleId) throws AppException {
        if (StringUtils.isEmpty(roleId)) {
            return JsonFailure.factory("请选择要删除的角色");
        }
        roleService.delRole(roleId);
        return JsonSuccess.factory("删除角色成功");
    }

    /**
     * 查询所有角色对应的菜单
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "findAllMenu", method = RequestMethod.POST)
    @ResponseBody
    public List<AuapRRoleMenuVo> findAllMenu(HttpServletRequest request) {
        String roleId = request.getParameter("roleId");
        List<AuapRRoleMenuVo> list = new ArrayList<AuapRRoleMenuVo>();
        if (StringUtils.isEmpty(roleId)) {
            return list;
        }
        return roleService.findAllMenu(roleId);
    }

    /**
     * 保存角色对应的菜单Id
     *
     * @param String
     * @return
     */
    @RequestMapping(value = "saveRoleMenuIds", method = RequestMethod.POST)
    @ResponseBody
    public String saveRoleMenuIds(String roleId, String menuIds) {
        if(StringUtils.split(menuIds,",").length > 0 && StringUtils.isNoneEmpty(roleId)){
            roleService.saveRoleMenuId(roleId, menuIds);
            return JsonSuccess.factory("保存角色菜单权限成功");
        }
        return JsonSuccess.factory("未选择任何菜单！");
    }

    /**
     * 通过empNo 查询用户角色
     * @param empNo
     * @return
     * @throws AppException 
     */
    @RequestMapping(value = "queryForEmpNo", method = RequestMethod.GET)
    @ResponseBody
    public String queryForEmpNo(String empNo) throws AppException {
    	if (StringUtils.isEmpty(empNo)) {
    		throw new AppException("请正确选择用户");
    	}
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("empNo", empNo);
    	List<AuapTRoleVo> auapTRoleList = roleService.queryForList(params);
    	if (Collections3.isNotEmpty(auapTRoleList)) {
    		return JsonSuccess.factory("roleNo", auapTRoleList.get(0).getRoleNo());
    	}
    	
    	return JsonSuccess.factory("roleNo","账号异常");
    }
}
